package com.mathset.presentation.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.mathset.bean.Member;
import com.mathset.business.service.IMailService;
import com.mathset.business.service.IMemberService;
import com.mathset.common.MathsetConstants;

@Controller
@RequestMapping("/mail")
public class MailController {

	@Autowired
	private IMailService iMailService;
	
	@Autowired
	private IMemberService iMemberService;
	
	@RequestMapping(value="/validateMail")
	public ModelAndView validateEmail(@RequestParam(value="token", required=true)String token,
			HttpSession session) {
		String email = iMailService.validateMail(token);
		if(email == null) {
			//TODO
		}
		iMailService.updateMemberMailValidated(email);
		Member member = iMemberService.retrieveMemberByMailAddress(email);
		session.setAttribute(MathsetConstants.MEMBER_ATTRIBUTE, member);
		return new ModelAndView("redirect:/home");
	}
}
